[FLINK-40491][core] Extend BinaryVariant with TIME, TIMESTAMP_NS, TIMESTAMP_LTZ_NS primitives - #29050
[FLINK-40491][core] Extend BinaryVariant with TIME, TIMESTAMP_NS, TIMESTAMP_LTZ_NS primitives#29050manner wants to merge 6 commits into
Conversation
…ESTAMP_LTZ_NS primitives
| assertThat(dateTimeVariant.getType()).isEqualTo(Variant.Type.TIMESTAMP_NS); | ||
| assertThat(dateTimeVariant.getDateTimeNanos()).isEqualTo(nanoLocalDateTime); | ||
| assertThat(dateTimeVariant.get()).isEqualTo(nanoLocalDateTime); | ||
| assertThatThrownBy(dateTimeVariant::getDateTime).isInstanceOf(VariantTypeException.class); |
There was a problem hiding this comment.
Try this case
LocalDateTime nanoLocalDateTime2 = LocalDateTime.of(2300, 1, 1, 0, 0, 0, 1);
Variant dateTimeVariant2 = builder.of(nanoLocalDateTime2);This will overflow. We should handle the error better
There was a problem hiding this comment.
Good catch! I'll add proper handling
There was a problem hiding this comment.
Actually the same error will currently happen when using a microsecond timestamp such as
LocalDateTime nanoLocalDateTime = LocalDateTime.of(2300, 1, 1, 0, 0, 0, 0);
Instant.until()s function for calculating microseconds between two instants just uses the function for calculating nanoseconds between two instants and then divides the result by 1000. This then also results in an overflow.

So this was a pre-existing bug, that only surfaced now.
|
after looking one more time the thing I didn't get I guess we should, or? |
Good point. We are planing to deliver the casting changes and support in a follow-up ticket/PR https://issues.apache.org/jira/browse/FLINK-40492 |
What is the purpose of the change
The variant binary encoding spec (see Variant Encoding) defines primitive type codes 17-20 for
TIME,TIMESTAMP_LTZ_NS,TIMESTAMP_NS, andUUIDthat are currently missing in Flink.This PR adds support for codes 17-19 (
TIME,TIMESTAMP_LTZ_NS,TIMESTAMP_NS) toBinaryVariant, so that aLocalTimeand a nanosecond-precisionInstant/LocalDateTimecan be represented in aVariantwithout lossy truncation to microseconds.UUID(code 20) is intentionally out of scope here and will be added in a separate ticket.Brief change log
BinaryVariantUtilconstants for primitive codes 17-19, extendedgetType()/valueSize()/getLong()to handle them, and added aTIME_FORMATTERfor JSON renderingVariant.Type.TIME/TIMESTAMP_NS/TIMESTAMP_LTZ_NSand the correspondinggetTime()/getDateTimeNanos()/getInstantNanos()accessors to theVariantinterfaceVariantBuilder.of(LocalTime); madeof(Instant)/of(LocalDateTime)precision-aware so a value with no sub-microsecond component keeps using the existing compact micros encoding, and only switches to the new nanosecond encoding when the value actually needs itappendTime/appendTimestampNanos/appendTimestampLtzNanosinBinaryVariantInternalBuilder, and the matching read/get()/toJson()support inBinaryVariantVerifying this change
This change added tests and can be verified as follows:
get()dispatch tests for the new types (BinaryVariantTest#testScalarVariant)Instant/LocalDateTimepick the existingTIMESTAMP_LTZ/TIMESTAMPencoding for microsecond-aligned values and the newTIMESTAMP_LTZ_NS/TIMESTAMP_NSencoding otherwise, including that the mismatched accessor throwsVariantTypeException(BinaryVariantTest#testNanosecondPrecisionVariant)LocalTimesilently truncates below microsecond precision, sinceTIMEhas no nanosecond-precision counterpart in the variant spec (BinaryVariantTest#testTimeSubMicrosecondTruncation)TIME/TIMESTAMP_NS/TIMESTAMP_LTZ_NS(BinaryVariantTest#testToJsonScalar)Does this pull request potentially affect one of the following parts:
@Public(Evolving): yes (VariantandVariantBuilderare@PublicEvolving; this adds new enum constants and new interface methods)Documentation
Was generative AI tooling used to co-author this PR?
Generated-by: Claude Sonnet 5 (Claude Code)